package com.glorysoft.xztpda.ui.login


import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.ui.MainActivity
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.databinding.ActivityLoginBinding
import com.glorysoft.xztpda.ui.setting.SettingActivity
import com.huawei.hms.hmsscankit.ScanUtil
import com.huawei.hms.ml.scan.HmsScan
import com.huawei.hms.ml.scan.HmsScanAnalyzerOptions
import kotlinx.android.synthetic.main.activity_login.*
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions

/**
 * FileName: LoginActivity
 * Author: XJH
 * Date: 2022/1/7 13:20
 * Description:
 * History:
 */
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>(), View.OnClickListener,
    EasyPermissions.PermissionCallbacks {
    override fun layoutId(): Int {
        return R.layout.activity_login
    }

    override fun initVariableId(): Int {
        return BR.loginViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        viewModel.initUser(this)
        viewModel.defUI.startActivity.observe(this) {
            startActivity(MainActivity::class.java)
            finish()
        }


    }

    override fun initData() {
        btnLogin.setOnClickListener(this)
        ivSetting.setOnClickListener(this)
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>?) {

    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("用户授权失败", false)
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms!!)) {
            AppSettingsDialog.Builder(this).build().show()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.btnLogin -> {
                viewModel.login()
            }
            R.id.ivSetting -> {
                startActivity(SettingActivity::class.java)
            }
        }
    }

}